import { Button, message } from 'antd';
import { getMessage, TextWidget } from '@inbiz/react';
import { routerPrefix } from 'InbizConfig';
import NewsListView from './view/list';
import NewsRollView from './view/roll';
import Detail from './view/detail';
import { connect } from '@formily/react';
import BizHeader from '@inbiz/basic/BizHeader';
import BizBody from '@inbiz/basic/BizBody';
import RollList from './view/rollList';
import './style.less';

export interface NewsItemProps {
  Title: string;
  Summary: string;
  ImgUrl: string;
  IsNew: boolean;
  Creator: string;
  CreateTime: string;
  ID: string;
  showCate: boolean;
  newsKey: News.INewsCateValueType;
  showField: Array<string>;
  showImage?: boolean;
  detail: string;
  ContentUrl?: URL;
  ContentCategory:string
}

export interface NewsProps {
  /**
   * @description 组件样式名，和style样式对应 ↓↓ eg: inbiz-News ↓↓
   */
  prefixCls: string;
  /**
   * @description 视图类型
   * @default static
   */
  viewType: string;
  /**
   * @description 标识
   * @default -
   */
  identification: string;
  /**
   * @description 标题
   * @default -
   */
  title: any;
  /**
   * @description 标题图标
   */
  icon: any;
  /**
   * @description 分类
   * @default -
   */
  newsKey: any;
  /**
   * @description 详细页
   * @default -
   */
  detail: string;
  /**
   * @description 记录数
   * @default 10
   */
  pageSize: number;
  /**
   * @description 显示图片
   * @default true
   */
  showImage?: boolean;
  /**
   * @description 显示分类名称
   * @default false
   */
  showCate?: boolean;
  /**
   * @description 突出显示新记录
   * @default true
   */
  standOut?: News.StandoutType;
  /**
   * @description 显示字段
   * @default createTime
   */
  showField?: Array<string>;
  /**
   * @description 自动滚动
   * @default true
   */
  autoRoll?: boolean;
  /**
   * @description 切换时间
   * @default 2s
   */
  delay?: number;
  /**
   * @description 列表图标
   * @default: -
   */
  listLogo?: string;
  /**
   * @description 扩展名称
   * @default: -
   */
  className?: string;
  /**
   * @description 高度
   * @default: -
   */
  height: number;
  /**
   * @description 标题字体大小
   * @default: 20
   */
  titlesize: number;
  /**
   * @description 标题图标
   * @default: -
   */
  titleLogo: News.LogoProps;
  /**
   * @description 标题文本颜色
   * @default: -
   */
  titlecolor: string;
  /**
   * @description 标题背景颜色
   * @default: -
   */
  titlebgcolor: string;
  /**
   * @description Biz样式
   * @default: -
   */
  style: any;
  /**
   * @description 详情页body内边距
   * @default: -
   */
  dedailPadding: string;
  /**
   * @description 更多 数据加载方式
   * @default: -
   */
  dataLoad: string;
  /**
   * @description 更多 显示文字
   * @default: -
   */
  showText: string;
  /**
   * @description 更多 文字显示位置
   * @default: -
   */
  showPosition: string;
  /**
   * @description 更多 关联页面
   * @default: -
   */
  correlationPage: string;
  emit: () => void,
  callBackEvents: {
    onParams: <T>(data: T) => Promise<T>,
    onRequest:<T> (data: object) => Promise<any>,
    onData: <T>(data: T) => Promise<T>
  }
}

const News = (props: NewsProps) => {
  const { viewType, prefixCls = 'inbiz-News', style = {},newsKey } = props;
  const isListView = ['static', 'resume', 'bigCard', 'smallCard'].includes(viewType);
  if (viewType === 'detail') {
    style.padding = props.dedailPadding;
  }

  const goMore = () => {
    const { origin, pathname, search } = window.location;
    if (!props.correlationPage) {
      message.warning(getMessage('news.tips.message_more_none'));
      return;
    }
    const [name] = props.correlationPage.split(',');
    window.open(`${origin}${pathname}${search}#${routerPrefix}${name}`);
  };
  const fixNewKey = Array.isArray(newsKey) ? newsKey :newsKey? [newsKey]:[];
  return (
    <div
      className={`${prefixCls} ${prefixCls}-${viewType}`}
      data-designer-node-id={props['data-designer-node-id']}
    >
      {viewType !== 'detail' && (
        <BizHeader
          {...props}
          extra={
            props.dataLoad === 'more' && props.showPosition === 'right-top' ? (
              <Button
                type="link"
                onClick={goMore}
                style={{ padding: 0, fontSize: 'var(--inbiz-font-size-base)', color: 'var(--inbiz-color-primary)' }}
              >
                <TextWidget>{props.showText || ''}</TextWidget>&nbsp;{'>'}
              </Button>
            ) : null
          }
        />
      )}
      <BizBody {...props} style={style}>
        {viewType === 'roll' && <RollList fixNewKey={fixNewKey} {...props} />}
        {viewType === 'roll-resume' && <NewsRollView {...props} />}
        {isListView && <NewsListView {...props} />}
        {viewType === 'detail' && <Detail fixNewKey={fixNewKey} {...props} />}
      </BizBody>
    </div>
  );
};

export default connect(News);
